<div class="layuimini-main">
	<div class="layui-form layuimini-form">
		<div class="layui-form-item">
			<label class="layui-form-label required">角色权限</label>
			<div class="layui-input-block">
				<table id="demoTreeTable1"></table>
			</div>
		</div>
		<input name="pid" type="hidden" />
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
			</div>
		</div>
	</div>
</div>
<script>
	var insTb;
	layui.use(['form', 'hsw','treeTable'], function() {
		var form = layui.form,
			hsw = layui.hsw,
			$ = layui.$;
		var treeTable = layui.treeTable;
		//监听提交
		form.on('submit(saveBtn)', function(data) {
			
			var role_pid = data.field.pid;
			
			var checkPids = insTb.checkStatus(); 
			
			var permission_pids = new Array();
			
			for (var x in checkPids){
				permission_pids[x] = checkPids[x].pid;
			}
			
			var json = {"role_pid":role_pid,"permission_pids":permission_pids};
			
			
			hsw.hsw_post_json(HSW_API_MANAGE_ROLE_AUTHORIZATION, JSON.stringify(json), function(data) {
			
				layer.msg('操作成功，即将刷新页面', {
					time: 3000 //
				}, function() {
					hsw.hsw_html_reload(0);
				});
				
			});
			return false;
		});
				
		window.authInit = function(data){
			
			$('input[name="pid"]').attr('value',data.rolePid);
			
			hsw.hsw_post(HSW_API_MANAGE_PEIMISSION_TREE, {}, function(treeData) {
			
				// 渲染表格
				insTb = treeTable.render({
				    elem: '#demoTreeTable1',
				    data: treeData,
				    tree: {
				        iconIndex: 2
				    },
				    cols: [
				        {type: 'checkbox'},
				        {field: 'pid', title: 'ID', width: 160},
				        {field: 'name', title: 'name', width: 240},
						{field: 'code', title: 'code', width: 240},
						{field: 'url', title: '地址', width: 180}
				    ],
				    style: 'margin-top:0;'
				});
				insTb.setChecked(data.permissionPids);
				insTb.expandAll();
				
			});
			
			form.render();
		}
		
	});
	
	
</script>
